package main

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/websocket"
	"log"
	"net/url"
	"time"
)

func main() {
	u := url.URL{Scheme: "ws", Host: ":9090", Path: "/ws"}
	c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
	if err != nil {
		log.Fatal("dial:", err)
	}
	defer c.Close()

	go func() {
		i := 0
		for {
			_, message, err := c.ReadMessage()
			i++
			if err != nil {
				log.Println("read:", err)
				return
			}
			log.Printf("recv: %s", message)
			if i%10 == 0 {
				recv := fmt.Sprintf("message recv:%d", i)
				fmt.Println(recv)
				b, err := json.Marshal(recv)
				if err != nil {
					fmt.Printf("failed to marshal message: %v", err)
				}
				err = c.WriteMessage(1, b)
				if err != nil {
					fmt.Printf("failed to write message: %v", err)
				}
			}
		}
	}()

	time.Sleep(20 * time.Second)
}
